package com.chukong.sdk.stat;

import java.util.HashMap;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.chukong.sdk.common.Log;
import com.chukong.sdk.provider.DBConstant;
import com.chukong.sdk.provider.DBHelper;
import com.chukong.sdk.serv.req.objs.ApkInfo;

public class StatManager {

    private Context mContext;
    private static StatManager sStatManager = null;
    private DBHelper mDBHelper;

    private StatManager(Context context) {
        mContext = context;
        mDBHelper = new DBHelper(mContext);
    }

    public static StatManager get(Context context) {
        if (sStatManager == null) {
            sStatManager = new StatManager(context);
        }
        return sStatManager;
    }

    public void updateDownloadTimes(String apkName, String pkgname) {
        SQLiteDatabase writable = mDBHelper.getWritableDatabase();
        String selection = DBConstant.STAT_PKGNAME + "='" + pkgname + "'";
        Cursor c = null;
        int _id = -1;
        int downTimes = 0;
        try {
            c = writable.query(DBConstant.TABLE_DOWNLOAD_STAT, null, selection, null, null, null, null);
            if (c != null && c.moveToFirst()) {
                _id = c.getInt(c.getColumnIndex(DBConstant._ID));
                downTimes = c.getInt(c.getColumnIndex(DBConstant.STAT_DOWNTIME));
            }
        } catch(Exception e) {
            Log.d(Log.TAG, "error : " + e);
        } finally {
            if (c != null) {
                c.close();
            }
        }
        downTimes ++;
        if (_id > -1) {
            ContentValues values = new ContentValues();
            String whereClause = DBConstant._ID + "=" + _id;
            writable.update(DBConstant.TABLE_DOWNLOAD_STAT, values, whereClause, null);
        } else {
            ContentValues values = new ContentValues();
            values.put(DBConstant.STAT_PKGNAME, pkgname);
            values.put(DBConstant.STAT_APKNAME, apkName);
            writable.insert(DBConstant.TABLE_DOWNLOAD_STAT, "foo", values);
        }
        writable.close();
    }

    public void insertDownload(ApkInfo apkInfo) {
        if (apkInfo == null) {
            return;
        }
        SQLiteDatabase writable = mDBHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(DBConstant.STAT_DOWNTIME, apkInfo.downloadTime);
        values.put(DBConstant.STAT_PKGNAME, apkInfo.packageName);
        values.put(DBConstant.STAT_APKNAME, apkInfo.apkName);
        values.put(DBConstant.STAT_APKLABEL, apkInfo.apkLabel);
        values.put(DBConstant.STAT_APKVERCODE, apkInfo.verCode);
        values.put(DBConstant.STAT_APKVERNAME, apkInfo.verName);
        writable.insert(DBConstant.TABLE_DOWNLOAD_STAT, "foo", values);
        writable.close();
    }

    public HashMap<String, Long> queryDowntimes() {
        SQLiteDatabase readable = mDBHelper.getReadableDatabase();
        Cursor c = null;
        try {
            String sql = String.format("select %s, count(*) as %s from download_stat group by pkgname;", DBConstant.STAT_PKGNAME, DBConstant.STAT_DOWNTIMES);
            c = readable.rawQuery(sql, null);
            if (c != null && c.moveToFirst()) {
                HashMap<String, Long> hasMap = new HashMap<String, Long>();
                do {
                    String pkgname = c.getString(c.getColumnIndex(DBConstant.STAT_PKGNAME));
                    long downTimes = c.getLong(c.getColumnIndex(DBConstant.STAT_DOWNTIMES));
                    hasMap.put(pkgname, downTimes);
                } while(c.moveToNext());
                return hasMap;
            }
        } catch(Exception e) {
            Log.d(Log.TAG, "error : " + e);
        } finally {
            if (c != null) {
                c.close();
            }
        }
        return null;
    }
}
